// pages/orderlist/payorder/payorder.js
var app = getApp();
var entry;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    needShow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    entry = '';
    if (options.entry) {
      entry = options.entry;
    }
    if (options.order) {
      var order = JSON.parse(decodeURIComponent(options.order));
      console.log(order);
      var orderinfo = {};
      orderinfo.order_sn = order.id;
      var goodsname = [];
      for (var i = 0; i < order.orderformOrderBillDOs[0].orderformOrderDetailDOs.length;i++){
        goodsname.push(order.orderformOrderBillDOs[0].orderformOrderDetailDOs[i].NAME + ' × ' + order.orderformOrderBillDOs[0].orderformOrderDetailDOs[i].COUNT);
      }
      var goodsnamestr = goodsname.join(',');
      orderinfo.goodsname = goodsnamestr;
      orderinfo.order_amount = app.keepTwoDecimalFull(order.payInfo.topay);
      this.setData({
        order: orderinfo
      });
      
      this.payOrderAction();
    }
    else if(options.cake){
      var cake = JSON.parse(decodeURIComponent(options.cake));
      console.log(cake);
      var payinfo = JSON.parse(decodeURIComponent(options.payinfo));
      console.log(payinfo);
      var goodsname = [];
      for(var i=0;i<cake.length;i++){
        if (cake[i].COUNT){
          goodsname.push(cake[i].name + ' × ' + cake[i].COUNT);
        }
        else{
          goodsname.push(cake[i].name + ' × ' + cake[i].num);
        }
      }
      var goodsnamestr = goodsname.join(',');
      var orderinfo = {
        goodsname: goodsnamestr,
        order_amount: app.keepTwoDecimalFull(payinfo.topay),
        order_sn: payinfo.lotNo
      };
      this.setData({
        order: orderinfo
      });

      this.payOrderAction();
    }
    else {
      wx.showToast({
        title: '无效订单',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  payOrderAction: function () {
    var url = '/weappmall/user/order/create';
    var trade_no = 'ZF' + this.data.order.order_sn;
    var openid = app.globalData.openID;
    var sessionid = app.globalData.sessionID;
    var totalfee = parseInt(parseFloat(this.data.order.order_amount) * 100);
    var params = {
      uname: " weappmall ",//固定值
      out_trade_no: trade_no,//订单号+ZF
      body: "商品购买",//固定内容
      spbill_create_ip: "127.0.0.1",
      trade_type: "JSAPI",
      openId: openid,
      attach: sessionid,
      total_fee: totalfee
    };
    app.loadDataFromServer(url, this.payOrderCallBack, params,'post');
  },
  payOrderCallBack: function (res) {
    console.log(res);
    if (res.code != '0000') {
      wx.showToast({
        title: res.message,
        icon: 'none'
      });
      return;
    }
    wx.showLoading({
      title: '',
    });
    var jsapi = res.data;
    var packagestr = "prepay_id=" + jsapi.prepay_id;
    wx.requestPayment({
      'appId': 'wx93176ec2a9cee7e1',
      'timeStamp': jsapi.timeStamp,
      'nonceStr': jsapi.nonceStr,
      'package': packagestr,
      'signType': 'MD5',
      'paySign': jsapi.paySign,
      'success': function (res) {
        wx.hideLoading();
        wx.showToast({
          title: '支付成功',
          duration: 1000
        });
        var delayid = setTimeout(function () {
          clearTimeout(delayid);
          wx.redirectTo({
            url: '../my/order/order?ordertype=3',
          });
        }, 1000);
      },
      'fail': function (res) {
        wx.hideLoading();
        wx.redirectTo({
          url: '../my/order/order?ordertype=2',
        });
      }
    });
  },
  waitPayOrderAction: function () {
    if (entry == 'neworder') {
      wx.redirectTo({
        url: '../my/order/order?ordertype=2',
      });
    }
    else {
      wx.navigateBack({});
    }
  }
})